"""
    Execution time calculation module
"""
import sys
import time

class Timing(object):
    
    def __init__(self, disabled=False, stream=None):
        self.disabled = disabled
        
        self.lap_name = ""
        self.lap_index = 1
        self.time = 0.0
        
        if stream is None or not isinstance(stream, file):
            self.stream = sys.stdout
    
    def out(self, *args):
        if self.disabled: return
        
        print >> self.stream, " ".join(args)
        
    def start(self, lap_name=None):
        if self.disabled: return
        
        self.time = time.time()
        self.lap_index += 1
        if lap_name is None:
            self.lap_name = "lap #%d" % self.lap_index
        else:
            self.lap_name = lap_name
            
        self.out("lap `%s' started" %self.lap_name)
        
    def stop(self):
        if self.disabled: return
        
        cur_time = time.time()
        dif = cur_time - self.time
        self.out("lap `%s' finished" % self.lap_name, ", time is:", "%0.12f" % dif)
        self.time = 0.0
            
        